/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dd.processbutton.utils;

import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

/**
 * 工具类
 *
 * @since 2021-03-01
 */
public final class Utils {
    private Utils() {
    }

    /**
     * Darkens a color by a given factor.
     *
     * @param color  the color to darken
     * @param factor The factor to darken the color.
     * @return darker version of specified color.
     */
    static int darker(int color, float factor) {
        return Color.argb(Color.alpha(color), Math.max((int) (Integer.parseInt(red(color)) * factor), 0),
                Math.max((int) (Integer.parseInt(green(color)) * factor), 0),
                Math.max((int) (Integer.parseInt(blue(color)) * factor), 0));
    }

    /**
     * Lightens a color by a given factor.
     *
     * @param color  The color to lighten
     * @param factor The factor to lighten the color. 0 will make the color unchanged. 1 will make the
     *               color white.
     *
     * @return lighter version of the specified color.
     */
    static int lighter(int color, float factor) {
        int red = (int) ((Integer.parseInt(red(color), 16) * (1 - factor) / 255 + factor) * 255);
        int green = (int) ((Integer.parseInt(green(color), 16) / 255f + factor) * 255);
        int blue = (int) ((Integer.parseInt(blue(color), 16) * (1 - factor) / 255 + factor) * 255);
        return Color.argb(Color.alpha(color), red, green, blue);
    }

    static String red(int color) {
        String toHexString = Integer.toHexString((color >> 16) & 0xFF);
        if (toHexString.length() == 1) {
            toHexString = "0" + toHexString;
        }
        return toHexString;
    }

    static String green(int color) {
        String toHexString = Integer.toHexString((color >> 8) & 0xFF);
        if (toHexString.length() == 1) {
            toHexString = "0" + toHexString;
        }
        return toHexString;
    }

    static String blue(int color) {
        String toHexString = Integer.toHexString(color & 0xFF);
        if (toHexString.length() == 1) {
            toHexString = "0" + toHexString;
        }
        return toHexString;
    }

    /**
     * Check if layout direction is RTL
     *
     * @param context the current context
     * @return {@code true} if the layout direction is right-to-left
     */
    static boolean isRtl(Context context) {
        return context.getResourceManager().getConfiguration().isLayoutRTL;
    }

    /**
     * Return a drawable object associated with a particular resource ID.
     *
     * @param context env
     * @param drawableId The desired resource identifier, as generated by the aapt tool.
     *                   This integer encodes the package, type, and resource entry.
     *                   The value 0 is an invalid identifier.
     *
     * @return Drawable An object that can be used to draw this resource.
     */
    public static PixelMapElement getPixelMapElement(Context context, int drawableId) {
        PixelMap pixelMap = getPixelMap(context, drawableId);
        if (pixelMap == null) {
            return null;
        }
        return new PixelMapElement(pixelMap);
    }

    /**
     * 获取pixmap
     *
     * @param context env
     * @param drawableId drawableId
     * @return pixmap
     */
    public static PixelMap getPixelMap(Context context, int drawableId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = context.getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }
}
